#include "net_config.h"
#include "Drv_W25Q64.h"  // 你自定义的 Flash 读写封装

//#define NET_CONFIG_ADDRESS     0x00080000UL
#define FLASH_ADDR_NET_CONFIG  0x00080000UL

NetConfig_TypeDef local_net_config;  // 这里定义了具体的变量，用来做全局变量

void LoadNetConfigFromFlash(void)
{
	  W25Q64_ReadData(NET_CONFIG_ADDRESS, (uint8_t *)&local_net_config, sizeof(NetConfig_TypeDef));  

    // 简单校验失败就恢复默认
    if (is_invalid_ip(local_net_config.auch_SIP) || is_invalid_mac(local_net_config.auch_SHA))
    {
			
        ResetNetConfigToDefault();
    }
		
		// 打印读取到的网络配置
    printf("=== Network Config Loaded from Flash ===\r\n");
    printf("MAC Address  : %02X:%02X:%02X:%02X:%02X:%02X\r\n",
           local_net_config.auch_SHA[0], local_net_config.auch_SHA[1], local_net_config.auch_SHA[2],
           local_net_config.auch_SHA[3], local_net_config.auch_SHA[4], local_net_config.auch_SHA[5]);

    printf("IP Address   : %d.%d.%d.%d\r\n",
           local_net_config.auch_SIP[0], local_net_config.auch_SIP[1],
           local_net_config.auch_SIP[2], local_net_config.auch_SIP[3]);

    printf("Gateway      : %d.%d.%d.%d\r\n",
           local_net_config.auch_GA[0], local_net_config.auch_GA[1],
           local_net_config.auch_GA[2], local_net_config.auch_GA[3]);
	
	printf("DNS      : %d.%d.%d.%d\r\n",
           local_net_config.auch_DNS[0], local_net_config.auch_DNS[1],
           local_net_config.auch_DNS[2], local_net_config.auch_DNS[3]);

    printf("Subnet Mask  : %d.%d.%d.%d\r\n",
           local_net_config.auch_SUB[0], local_net_config.auch_SUB[1],
           local_net_config.auch_SUB[2], local_net_config.auch_SUB[3]);

    printf("========================================\r\n");
		
}

//保存网络设置到flash中
void SaveNetConfigToFlash(void)
{
    W25Q64_SectorErase(NET_CONFIG_ADDRESS);	
    W25Q64_PageProgram(NET_CONFIG_ADDRESS, (uint8_t*)&local_net_config, sizeof(local_net_config));  
}

//网络设置的默认值
void ResetNetConfigToDefault(void)
{
	  //默认地址192.168.0.88
    local_net_config.auch_SIP[0] = 192;
    local_net_config.auch_SIP[1] = 168;
    local_net_config.auch_SIP[2] = 0;
    local_net_config.auch_SIP[3] = 88;

    local_net_config.auch_SUB[0] = 255;
    local_net_config.auch_SUB[1] = 255;
    local_net_config.auch_SUB[2] = 255;
    local_net_config.auch_SUB[3] = 0;

    local_net_config.auch_GA[0] = 192;
    local_net_config.auch_GA[1] = 168;
    local_net_config.auch_GA[2] = 0;
    local_net_config.auch_GA[3] = 1;
	
	local_net_config.auch_DNS[0] = 114;
	local_net_config.auch_DNS[1] = 114;
    local_net_config.auch_DNS[2] = 114;
    local_net_config.auch_DNS[3] = 114;

    local_net_config.auch_SHA[0] = 0x54;
    local_net_config.auch_SHA[1] = 0x43;
    local_net_config.auch_SHA[2] = 0x00;
    local_net_config.auch_SHA[3] = 0x00;
    local_net_config.auch_SHA[4] = 0x00;
    local_net_config.auch_SHA[5] = 0x02;

    //local_net_config.dhcp_enable = 0;
	SaveNetConfigToFlash();
}

//检索ip是否合法
bool is_invalid_ip(uint8_t ip[4])
{
    // 无效条件说明：
    // 1. 全0 或 全255
    // 2. 0.x.x.x 是保留地址（本地网络无法使用）
    // 3. 127.x.x.x 是本地回环地址
    // 4. 224-255 是 D/E 类地址（广播、多播）
    return (
        (ip[0] == 0) ||                                 // 0.x.x.x（保留地址）
        (ip[0] == 127) ||                               // 回环地址
        (ip[0] >= 224) ||                               // D/E类
        (ip[0] == 255 && ip[1] == 255 && 
         ip[2] == 255 && ip[3] == 255) ||               // 全 255
        (ip[0] == 0 && ip[1] == 0 && 
         ip[2] == 0 && ip[3] == 0)                      // 全 0
    );
}


//检索mac是否合法
bool is_invalid_mac(uint8_t mac[6])
{
    return (
        (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0) ||
        (mac[0] == 0xFF && mac[1] == 0xFF && mac[2] == 0xFF &&
         mac[3] == 0xFF && mac[4] == 0xFF && mac[5] == 0xFF)
    );
}
